﻿using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace HYRElectric.Equipment
{
	public class EquipmentLayout
	{
		public EquipmentLayout(UIDocument uiDoc, Document document, DbFamilyObject familyObject, FamilySymbol selectedFamilySymbol, List<BuiltInCategory> lstFilter)
		{
			this.docUI = uiDoc;
			this.doc = document;
			this.dbFamilyObject = familyObject;
			this.familySymbol = selectedFamilySymbol;
			this.categoryFilters = lstFilter;
		}

		public virtual bool Layout()
		{
			if (this.familySymbol == null)
			{
				this.familySymbol = this.GetFamilySymbol();
			}
			return this.familySymbol != null;
		}

		private void SetFamilyParameter(Element element, int nSizeStyle)
		{
			IList<Parameter> parameters = element.GetParameters("实际尺寸图例");
			if (parameters.Count > 0)
			{
				if (nSizeStyle == 2)
				{
					parameters[0].Set(1);
				}
				else if (nSizeStyle == 1)
				{
					parameters[0].Set(0);
				}
			}
			parameters = element.GetParameters("固定尺寸图例");
			if (parameters.Count > 0)
			{
				if (nSizeStyle == 1)
				{
					parameters[0].Set(1);
					return;
				}
				if (nSizeStyle == 2)
				{
					parameters[0].Set(0);
				}
			}
		}

		protected Family LoadFamily()
		{
			Family result = null;
			string text = Path.GetFileNameWithoutExtension(this.dbFamilyObject.Family);
			if (text == "")
			{
				text = this.dbFamilyObject.Name;
			}
			if (this.IsFamilyIsLoaded(ref result, text, this.doc))
			{
				return result;
			}
			if (this.doc == null || this.dbFamilyObject == null || this.categoryFilters.Count <= 0 || !File.Exists(this.dbFamilyObject.Family))
			{
				return result;
			}
			Transaction transaction = null;
			try
			{
				foreach (BuiltInCategory builtInCategory in this.categoryFilters)
				{
					foreach (Element element in YJKElementFilter.GetElementByCategory(this.doc, typeof(FamilySymbol), builtInCategory))
					{
						FamilySymbol familySymbol = element as FamilySymbol;
						if (familySymbol.Family.Name == text)
						{
							return familySymbol.Family;
						}
					}
				}
				transaction = new Transaction(this.doc, "LoadFamily");
				transaction.Start();
				this.doc.LoadFamily(this.dbFamilyObject.Family, out result);
				transaction.Commit();
			}
			catch (Exception)
			{
			}
			finally
			{
				if (transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		protected bool IsFamilyIsLoaded(ref Family hsFamilyCur, string strFamilyName, Document doc)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(doc).OfClass(typeof(Family)).ToElements();
			hsFamilyCur = null;
			foreach (Element element in enumerable)
			{
				Family family = (Family)element;
				if (family.Name.ToUpper().Trim().CompareTo(strFamilyName.ToUpper().Trim()) == 0)
				{
					hsFamilyCur = family;
					return true;
				}
			}
			return false;
		}

		public FamilySymbol GetFamilySymbol()
		{
			FamilySymbol result = null;
			Family family = this.LoadFamily();
			if (family != null)
			{
				using (List<FamilySymbol>.Enumerator enumerator = family.GetSymbols().GetEnumerator())
				{
					if (enumerator.MoveNext())
					{
						result = enumerator.Current;
					}
				}
			}
			return result;
		}

		public bool SetParametersValue(ElementId id, int nSizeStyle = 0)
		{
			Element elementById = this.doc.GetElementById(id);
			return this.SetParametersValue(elementById, nSizeStyle);
		}

		public bool SetParametersValue(Element element, int nSizeStyle = 0)
		{
			if (element == null)
			{
				return false;
			}
			bool result;
			try
			{
				ElementParameter.SetParameterValueEx(element, "名称", this.dbFamilyObject.Name, (Autodesk.Revit.DB.ParameterType)1);
				ElementParameter.SetLevel(element, 0.0);
				foreach (DbFamilyParameter dbFamilyParameter in this.dbFamilyObject.Parameters)
				{
					if (dbFamilyParameter.SharedName == "保护半径")
					{
						ElementParameter.SetParameterValueEx(element, dbFamilyParameter.SharedName, dbFamilyParameter.Value, (Autodesk.Revit.DB.ParameterType)3);
					}
					else
					{
						ElementParameter.SetParameterValueEx(element, dbFamilyParameter.SharedName, dbFamilyParameter.Value, (Autodesk.Revit.DB.ParameterType)1);
					}
					if (dbFamilyParameter.SharedName == "安装高度")
					{
						double level = Convert.ToDouble(dbFamilyParameter.Value);
						ElementParameter.SetLevel(element, level);
					}
				}
				if (this.categoryFilters.Contains(BuiltInCategory.OST_LightingFixtures))
				{
					object parameterValue = this.GetParameterValue("LightPower");
					object parameterValue2 = this.GetParameterValue("LightCount");
					if (parameterValue != null && parameterValue2 != null)
					{
						double num = Convert.ToDouble(parameterValue) * (double)Convert.ToInt32(parameterValue2);
						ElementParameter.SetParameterValueEx(element, "功率", num, (Autodesk.Revit.DB.ParameterType)1);
					}
				}
				this.SetFamilyParameter(element, nSizeStyle);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		protected object GetParameterValue(string name)
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				return null;
			}
			if (!this.dbFamilyObject.Parameters.Exists((DbFamilyParameter param) => param.Name == name))
			{
				return null;
			}
			DbFamilyParameter dbFamilyParameter = this.dbFamilyObject.Parameters.Find((DbFamilyParameter param) => param.Name == name);
			if (dbFamilyParameter != null)
			{
				return dbFamilyParameter.Value;
			}
			return null;
		}

		protected UIDocument docUI;

		protected Document doc;

		protected ExternalCommandData m_data;

		protected DbFamilyObject dbFamilyObject;

		protected List<BuiltInCategory> categoryFilters = new List<BuiltInCategory>();

		protected FamilySymbol familySymbol;
	}
}
